package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HuangFu
 * @Date: 2020/2/14 19:54
 * @Description: 购物车服务
 */
@Service
public class CartServiceImpl implements CartService {

    private final static String  cart = "cart_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private SpuFeign spuFeign;

    @Override
    public void add(String skuId, int num, String username) {
        //获取redis中的数据sku数据
        OrderItem orderItem = (OrderItem)redisTemplate.boundHashOps(cart + username).get(skuId);
        //校验数据是否存在
        if(orderItem!=null){
            //设置数量
            orderItem.setNum(orderItem.getNum()+num);
            //商品小于1时删除数据
            if(orderItem.getNum()<=0){
                redisTemplate.boundHashOps(cart + username).delete(skuId);
            }
            //刷新付款
            orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());
        }else{
            Result<Sku> skuResult = skuFeign.findById(skuId);
            Sku sku = skuResult.getData();
            Result<Spu> spuResult = spuFeign.findSpuById(sku.getSpuId());
            Spu spu = spuResult.getData();
            orderItem = this.skuToOrderItem(sku, spu, num);

        }
        //存储到redis
        redisTemplate.boundHashOps(cart+username).put(skuId,orderItem);

    }

    @Override
    public Map list(String username) {
        Map map = new HashMap(16);

        //获取redis中的速度
        List<OrderItem> orderItems = redisTemplate.boundHashOps(cart + username).values();
        map.put("orderItemList",orderItems);

        //商品总数
        Integer totalNum = 0;
        //商品总价格
        Integer totalMoney = 0;
        for (OrderItem orderItem : orderItems) {
              totalNum += orderItem.getNum();
              totalMoney += orderItem.getPayMoney();
        }
        map.put("totalNum",totalNum);
        map.put("totalMoney",totalMoney);

        return map;
    }

    /**
     * sku转换为orderItem
     * @param sku
     * @param spu
     * @param num
     * @return
     */
    private OrderItem skuToOrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num*orderItem.getPrice());
        //单价*数量
        orderItem.setPayMoney(num*orderItem.getPrice());
        //实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight()*num);
        //重量=单个重量*数量
        // 分类ID设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
